﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Workflow.Interface;
using MyLib;
using Workflow.Entity;
using Workflow.Logic;

namespace Workflow
{
    public partial class OpenForm : System.Web.UI.Page
    {
        ITask _task = ServiceContainer.Instance().GetService<ITask>();
        protected void Page_Load(object sender, EventArgs e)
        {
            string taskID = Request.QueryString["TaskID"];
            string type = Request.QueryString["Type"];
            string processName = Request.QueryString["ProcessName"];
            string incident = Request.QueryString["Incident"];
            string stepName = Request.QueryString["StepName"];
            string username = string.IsNullOrEmpty(Page.User.Identity.Name) ? ConvertUtil.ToString(Session["userAccount"]) : Page.User.Identity.Name;
            string url = "";
            if (type != null && type.ToUpper().Trim() == "DRAFT")
            {
                if (processName != null && processName.Trim() != "")
                {
                    string strSql = "select c.initiateid from initiate c where c.processname = '" + processName + "'";
                    taskID = ConvertUtil.ToString(DataAccess.Instance("UltDB").ExecuteScalar(strSql)).Trim();
                }
                string page = StepSettingsLogic.GetDraftPage(processName);
                url = "http://" + HttpContext.Current.Request.Url.Host + ":"
                    + HttpContext.Current.Request.Url.Port + "/" + page + "?ProcessName=" + processName + "&StepName=" + stepName + "&Incident="
                   + incident + "&TaskID=" + taskID + "&UserName=" + HttpContext.Current.Server.UrlEncode(username) + "&Type=" + type;
            }
            else
            {
                url = _task.GetTaskUrl(taskID, type, username.Replace("\\", "/"));
            }
            Response.Redirect(url);

        }
    }
}